package com.sgb.zhaocai.view.demand.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.link.entity.EventConstant;
import com.sgb.link.entity.LoginEvents;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.FragmentDemandForecastBinding;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.refresh.SuperSwipeRefreshLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;

/**
 * Time:2021/8/13
 * AUTHOR:F_Q
 * Description:招采首页-需求预告
 */
public class DemandForecastFragment extends AppLazyFragment {
    private FragmentDemandForecastBinding binding;
    private String[] mTitles = {"工程项目", "材料采购", "机械租赁", "设备租赁", "劳务用工", "分包计划"};
    private ArrayList<Fragment> mFragments;
    private SlidingFragmentPagerAdapter mPagerAdapter;
    private String longitude;
    private String latitude;
    private SuperSwipeRefreshLayout swipeRefreshMainRecruitmentFragment;

    public static DemandForecastFragment newInstance() {
        DemandForecastFragment fragment = new DemandForecastFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_demand_forecast, container, false);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return binding.getRoot();
    }


    @Override
    protected void initView() {
        super.initView();
        initViewPager();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvents event) {
        if (event == null) {
            return;
        }
       if (event.getMessageTag() == EventConstant.EVENT_LOGIN_OUT||event.getMessageTag() == EventConstant.EVENT_LOGIN) {
           initView();
        }
    }
    private void initViewPager() {
        mFragments = new ArrayList<>();
        mFragments.add(ProjectFragment.newInstance("2"));//工程项目
        mFragments.add(MaterialsBuyFragment.newInstance("2")); //材料采购
        mFragments.add(LeaseNeedFragment.newInstance("2", "1"));//机械租赁
        mFragments.add(LeaseNeedFragment.newInstance("2", "2"));//设备租赁
        mFragments.add(LabourWorkFragment.newInstance("2"));//劳务用工
        mFragments.add(SubpackagePlanFragment.newInstance("2"));//分包计划
        mPagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitles);
        binding.viewPager.setOffscreenPageLimit(mTitles.length);
        binding.viewPager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
    }

    @Override
    protected void onLazyLoad() {
        int pos = binding.tabLayout.getSelectedTabPosition();
        Fragment fragment = mFragments.get(pos);
        if (fragment != null && fragment instanceof ProjectFragment) {
            ProjectFragment projectFragment = ((ProjectFragment) fragment);
            if (projectFragment.isInIt) {
                projectFragment.onLazyLoad();
            }
        }
    }

    public void onPullRefresh(SuperSwipeRefreshLayout swipeRefreshMainRecruitmentFragment) {
        this.swipeRefreshMainRecruitmentFragment = swipeRefreshMainRecruitmentFragment;
        swipeRefreshMainRecruitmentFragment.setLoadMore(false);
        int pos = binding.tabLayout.getSelectedTabPosition();
        Fragment fragment = mFragments.get(pos);
        if (fragment != null && fragment instanceof ProjectFragment) {
            ProjectFragment projectFragment = ((ProjectFragment) fragment);
            projectFragment.onLazyLoad();
        }
        if (fragment != null && fragment instanceof MaterialsBuyFragment) {
            MaterialsBuyFragment MaterialsBuyFragment = ((MaterialsBuyFragment) fragment);
            MaterialsBuyFragment.onLazyLoad();
        }
        if (fragment != null && fragment instanceof LeaseNeedFragment) {
            LeaseNeedFragment LeaseNeedFragment = ((LeaseNeedFragment) fragment);
            LeaseNeedFragment.onLazyLoad();
        }
        if (fragment != null && fragment instanceof LabourWorkFragment) {
            LabourWorkFragment LabourWorkFragment = ((LabourWorkFragment) fragment);
            LabourWorkFragment.onLazyLoad();
        }
        if (fragment != null && fragment instanceof SubpackagePlanFragment) {
            SubpackagePlanFragment SubpackagePlanFragment = ((SubpackagePlanFragment) fragment);
                SubpackagePlanFragment.onLazyLoad();
        }

    }

}
